import { createRouter, createWebHistory } from 'vue-router'
import Login from '../views/LoginView.vue'
import Layout from '../layout/Layout.vue'
import store from '@/store/index'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'login',
      component: Login
    },
    {
      path: '/home',
      name: 'Layout',
      component: Layout,
      redirect: "/assetsStatistics",
      children: [
        {
          path: '/assetsStatistics',
          name: 'assetsStatistics',
          meta: { isAuth: true },
          component: () => import("@/views/AssetsStatistics.vue")
        },
        {
          path: '/accountManagement',
          name: 'accountManagement',
          meta: { isAuth: true },
          component: () => import("@/views/AccountManagement.vue")
        },
        {
          path: '/fundsList',
          name: 'fundsList',
          meta: { isAuth: true },
          component: () => import("@/views/Fund/FundsList.vue")
        },
        {
          path: '/systemSetting',
          name: 'systemSetting',
          meta: { isAuth: true },
          component: () => import("@/views/SystemSetting.vue")
        },
        {
          path: '/investmentList',
          name: 'investmentList',
          meta: { isAuth: true },
          component: () => import("@/views/Fund/InvestmentList.vue")
        },
        {
          // 添加通配符路径
          path: '/:pathMatch(.*)*',
          name: 'notFound',
          component: () => import('@/views/404.vue')
        }
      ]
    }
  ]
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  if (to.meta.isAuth) {
    if (store.state.loginStatus != 'yes') {
      ElMessage.error("没有权限访问")
      next('/')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router